package com.cn.web.controller.EM.partRegt;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.vo.EM.partRegt.*;
import com.cn.common.utils.JsonConverter;
import com.cn.mapper.dao.db5.EM.partRegt.Send_back_sub_tDao;
import com.cn.mapper.dao.db5.EM.partRegt.Send_back_tDao;
import com.cn.service.server.EM.partRegt.Send_back_tDaoService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
@RestController
@Transactional
@RequestMapping("/send")
public class Send_back_tController {
    private final Send_back_tDaoService send_back_tDaoService;
    private final Send_back_tDao send_back_tDao;
    private final Send_back_sub_tDao send_back_sub_tDao;

    @Autowired(required = false)
    public Send_back_tController(Send_back_sub_tDao send_back_sub_tDao, Send_back_tDaoService send_back_tDaoService, Send_back_tDao send_back_tDao) {
        this.send_back_tDaoService = send_back_tDaoService;
        this.send_back_tDao = send_back_tDao;
        this.send_back_sub_tDao=send_back_sub_tDao;
    }

    @RequestMapping("/getPartSingleRegister")
    @ResponseBody
    public List<Send_back_sub_tBean> getPartSingleRegister(@RequestBody JSONObject jsonObject) throws JsonProcessingException {

//        JSONObject info = jsonObject.getJSONObject("info");
//        if (info != null) {
//            Object input1Value = info.get("input1");
//            Object input2Value = info.get("input2");
//            Object input3Value = info.get("input3");
//            Object input4Value = info.get("input4");
//            Object input6Value = info.get("input6");
//            info.remove("input1");
//            info.remove("input2");
//            info.remove("input3");
//            info.remove("input4");
//            info.remove("input6");
//            info.put("modelName", input1Value);
//            info.put("modelName", input2Value);
//            info.put("modelName", input3Value);
//            info.put("modelName", input4Value);
//            info.put("modelName", input6Value);
//
//
//
//            jsonObject.put("info", info);
//        }



//        send_back_tDaoService.getPartSingleRegister()
//        Send_back_sub_tBean send_back_sub_tBean = new Send_back_sub_tBean();
//        PageInfo pageInfo = new PageInfo();
//        String limit = jsonObject.getString("limit");
//        String page = jsonObject.getString("page");
//        String escName = jsonObject.getString("input1");
//        String structureName = jsonObject.getString("input2");
//        String modelName = jsonObject.getString("input3");
//        String uniqueCode = jsonObject.getString("input4");
//        String ftype = jsonObject.getString("input6");
//        pageInfo.setPage(page);
//        pageInfo.setLimit(limit);
//        send_back_sub_tBean.setEcsName(escName);
//        send_back_sub_tBean.setStructureName(structureName);
//        send_back_sub_tBean.setModelName(modelName);
//        send_back_sub_tBean.setUniqueCode(uniqueCode);
//        send_back_sub_tBean.setFtype(ftype);
//        System.out.println("cccccccccc"+ftype);
//        List<Send_back_sub_tBean> partSingleRegister = send_back_tDaoService.getPartSingleRegister(send_back_sub_tBean, pageInfo);
//        for (Send_back_sub_tBean sendSubList : partSingleRegister) {
//            System.out.println("xaaaaaaaaaaaaaa"+sendSubList);
//
//        }
        System.out.println("aaaasssssssss"+jsonObject);
        List<Send_back_sub_tBean> partSingleRegister = send_back_sub_tDao.getPartSingleRegister(String.valueOf(jsonObject));
        return partSingleRegister;
    }

    @RequestMapping("/insert")
    @ResponseBody
    public BarkPar insert(@RequestBody JSONObject jsonObject) throws JsonProcessingException, ParseException {
        String time = jsonObject.getString("time");
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String formattedTime = sdf.format(new Date(Long.parseLong(time)));
        Send_back_tVo formMainTable = jsonObject.getObject("formMainTable", Send_back_tVo.class);
        formMainTable.setFdate(formattedTime);
        JSONArray childList = jsonObject.getJSONArray("childList");
        List<Send_back_tVo> send_back_sub_tBeanList = childList.toJavaList(Send_back_tVo.class);
        for (int i = 0; i < send_back_sub_tBeanList.size(); i++) {
            String num = String.valueOf(i + 1);
            send_back_sub_tBeanList.get(i).setRowNum(num);
        }

        String jsonString = JsonConverter.convertToCustomJson(formMainTable, send_back_sub_tBeanList, null);


        return send_back_tDao.insert(jsonString);
    }

    @RequestMapping("/getList")
    @ResponseBody
    public List<Send_back_tBean> getList(@RequestBody JSONObject jsonObject) throws JsonProcessingException {
        String code = jsonObject.getString("code");//单据编号
        String bfdate = jsonObject.getString("bfdate");//开始时间
        String efdate = jsonObject.getString("efdate");//结束时间
        String userName = jsonObject.getString("userName");//业务员名称
        String state = jsonObject.getString("state");//状态
        String isRatify = jsonObject.getString("isRatify");//是否需要签批
        String ratifyState = jsonObject.getString("ratifyState");//签批状态
        String codeSingle = jsonObject.getString("codeSingle");//codeSingle,单据编号的精确查询
        Send_back_tBean send_back_tBean = new Send_back_tBean();
        send_back_tBean.setCode(code);
        send_back_tBean.setBfdate(bfdate);
        send_back_tBean.setEfdate(efdate);
        send_back_tBean.setUserName(userName);
        send_back_tBean.setState(state);
        send_back_tBean.setIsRatify(isRatify);
        send_back_tBean.setRatifyState(ratifyState);
        send_back_tBean.setCodeSingle(codeSingle);

        String jsonString = JsonConverter.convertToCustomJson(send_back_tBean, null, null);
        System.out.println(jsonString);

        return send_back_tDao.getList(jsonString);
    }

    @RequestMapping("/getListInfo")
    @ResponseBody
    public List<Send_back_tVo> getListInfo(@RequestBody JSONObject jsonObject) throws JsonProcessingException {

        String id = jsonObject.getString("id");
        Send_back_tBean send_back_tBean = new Send_back_tBean();
        send_back_tBean.setID(id);

        String jsonString = JsonConverter.convertToCustomJson(send_back_tBean, null, null);
        System.out.println(jsonString);
        List<Send_back_tVo> listInfo = send_back_tDao.getListInfo(jsonString);
        return listInfo;
    }

    @RequestMapping("/upDate")
    @ResponseBody
    public BarkPar upDate(@RequestBody JSONObject jsonObject) throws JsonProcessingException, ParseException {

        System.out.println("jsonObject:xxxxxxxxxxxxxxxxxxx" + jsonObject);
        send_back_tDao.insert(String.valueOf(jsonObject));
        return send_back_tDao.insert(String.valueOf(jsonObject));
    }

    @RequestMapping("/send_back_state_update")
    @ResponseBody
    public BarkPar send_back_state_update(@RequestBody JSONObject jsonObject) throws JsonProcessingException, ParseException {
        System.out.println("jsonObject:aaaaaaaaaaaaaaa" + jsonObject);


        return send_back_tDao.send_back_state_update(String.valueOf(jsonObject));

    }
}
